package com.loe.ui.api

import android.view.ViewGroup
import android.webkit.JavascriptInterface
import com.loe.mvp.ext_java.safe
import com.loe.ui.jsContext
import com.loe.ui.registerFunReturn
import com.quickjs.JSObject

open class ViewGroupApi(private val view: ViewGroup, jsObject: JSObject = JSObject(jsContext)) : ViewApi(view, jsObject)
{
    val uiViewGroup = view as IUiViewGroup

    @JavascriptInterface
    fun addChild(child: JSObject)
    {
        safe { uiViewGroup.addChild(child) }
    }

    @JavascriptInterface
    open fun removeAllChild()
    {
        view.removeAllViews()
    }

    init
    {
        jsObject.registerFunReturn("findView")
        {
            safe { return@registerFunReturn uiViewGroup.findObject(it.getString(0)) }
        }.let {
            jsObject.set("findViewGroup", it)
            jsObject.set("findTextView", it)
            jsObject.set("findEditText", it)
            jsObject.set("findImageView", it)
            jsObject.set("findListView", it)
            jsObject.set("findScrollView", it)
            jsObject.set("findViewPager", it)
        }
    }
}